class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int ret = 0;
        queue<int> q;
        for (int i = 0; i < tickets.size(); i++)
            q.push(i);
        while (tickets[k] != 0)
        {
            int front = q.front();
            tickets[front]--;
            q.pop();
            ret++;
            if (tickets[front] > 0)
                q.push(front);
        }
        return ret;
    }
};